/**
 *@description:TODO
 * @author: cai
 *@time:2017年10月12日上午11:49:59
 */
package com.pos.modules;

import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.pos.BaseActivity;
import com.pos.R;
import com.pos.net.base.Request.Method;
import com.pos.net.netparam.NetParams;
import com.pos.net.netparam.RequestUrls;
import com.pos.net.request.interfa.BaseRequestListener;
import com.pos.net.request.interfa.JsonRequestListener;
import com.pos.widgets.dialog.CommonDialog;

import org.json.JSONObject;

/**
 * @description:闪屏界面(检查更新)
 * @author cai
 * @time:2017年10月12日上午11:49:59
 */
public class SplashActivity extends BaseActivity {
	private LocationClient mLocClient;
	public MyLocationListenner myListener = new MyLocationListenner();
	private boolean hasNewVersion = false, forceUpgrade = false;
	private String newVersionHint, upgradeUrl;
	private CommonDialog commonDialog;

	@Override
	public void initView() {
		setContentView(R.layout.splash_layout);

//		long fen = 98765432100L;
//		System.out.println(FinancialUtils.fenToYuan(fen) + "");
//		System.out.println(FinancialUtils.fen2yuan(fen) + "");
	}

	@Override
	public void initData() {
		commonDialog = new CommonDialog(mContext);
		commonDialog.setCancelable(false);

		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);// 打开gps
		option.setCoorType("bd09ll"); // 设置坐标类型
		option.setScanSpan(800);
		option.setAddrType("all");
		mLocClient = new LocationClient(this);
		mLocClient.setLocOption(option);
		mLocClient.registerLocationListener(myListener);
		mLocClient.start();
		// NetParams.longitude = 30.259244;
		// NetParams.latitude = 120.219375;
		netRequest.startRequest(RequestUrls.hostUrl +
		 RequestUrls.checkNewVersion, Method.POST,
		 NetParams.checkNewVersion(mContext), 0, listener);
	}

	@Override
	public void initListener() {
		// TODO Auto-generated method stub

	}

	private BaseRequestListener listener = new JsonRequestListener() {

		@Override
		public void onStart(int requestId) {

		}

		@Override
		public void onError(int requestId, int errorCode, String errorMsg) {
			startActivity(LoginActivity.class);
			finish();
		}

		@Override
		public void onSuccess(int requestId, String url, JSONObject jsonResult) {
			Log.e("TAG", "onSuccess: "+jsonResult.toString() );
			Log.e("TAG", "onSuccess1111111111: "+jsonResult.toString() );
			int retCode = jsonResult.optInt("retCode", 0);
			retCode = 0;
			if (retCode == 0) {
				hasNewVersion = jsonResult.optBoolean("hasNewVersion", false);
				// hasNewVersion = true;
				if (!hasNewVersion) {
					startActivity(LoginActivity.class);
					finish();
				} else {
					forceUpgrade = jsonResult.optBoolean("forceUpgrade", false);
					newVersionHint = jsonResult.optString("newVersionHint");
					downLoadMsg = newVersionHint;
					upgradeUrl = jsonResult.optString("upgradeUrl");
					downLoadUrl = upgradeUrl + "&os=AndroidPOS&deviceType=" + Build.MODEL + "&longitude="
							+ NetParams.longitude + "&latitude=" + NetParams.latitude;
					// downLoadMsg = "这只是个例子";
					// downLoadUrl =
					// "http://gdown.baidu.com/data/wisegame/43e76ce22df64c52/QQ_730.apk";
					if (!forceUpgrade) {
						// 非强制升级
						commonDialog.showDialog("发现新版本\n" + newVersionHint, "升级", new OnClickListener() {

							@Override
							public void onClick(View v) {
								commonDialog.dismissDialog();
								startUpdate();
							}
						}, new OnClickListener() {

							@Override
							public void onClick(View v) {
								commonDialog.dismissDialog();
								startActivity(LoginActivity.class);
								finish();
							}
						});

					} else {
						// 强制升级
						startUpdate();
					}
				}
			} else {
				startActivity(LoginActivity.class);
				finish();
			}

		}
	};

	/**
	 * 定位SDK监听函数
	 */
	public class MyLocationListenner implements BDLocationListener {
		@Override
		public void onReceiveLocation(BDLocation location) {
			if (location == null)
				return;

			NetParams.longitude = location.getLongitude();
			NetParams.latitude = location.getLatitude();
			mLocClient.stop();
/*
			netRequest.startRequest(RequestUrls.hostUrl + RequestUrls.checkNewVersion, Method.POST,
					NetParams.checkNewVersion(mContext), 0, listener);*/

		}
	}

}
